from django.shortcuts import render
from django.views.decorators.http import *
from kkoffer.user.models import *
from kkoffer.user.models import education as e
from kkoffer.user.models import work as w
from kkoffer.user.models import member
from django.http import HttpResponse
from time import time
from django.views.generic import View
from django.conf import settings
from django.db import connection
from django.http import HttpResponseRedirect
from .forms import *
from datetime import date
from django.utils.translation import ugettext as _

class my(View):
	def get(self,request):
		uid = request.session.get('id')
		id = request.GET.get('id','')
		if not id:
			context = {}
			obj = member.objects.filter(uid = uid)
			if obj:
				context = obj.values()[0]
				context['person'] = member.objects.get(uid = uid)
				context['education'] = e.objects.filter(uid = uid)
				context['work'] = w.objects.filter(uid = uid)
				# import ipdb; ipdb.set_trace()
				context['age'] = date.today().year - context['birthday'].year
			# 
			return render(request,'my.html',context)
		else:
			context = {}
			if member.objects.filter(uid = uid):
				context = member.objects.filter(uid = uid).values()[0]
			context['education'] = e._meta.get_field('education').choices
			return render(request,'person.html',context)

		
	def post(self,request):
		id = request.session['id']
		obj = member.objects.filter(uid_id = id)
		post = request.POST.copy()
		post['uid'] = request.session['id'] 
		if obj:
			f = memberForm(post, instance=obj[0])
		else:
			f = memberForm(post)
		if f.is_valid():
			f.save()
			name = request.POST['realName']
			obj = user.objects.get(id=id)
			obj.name = name
			obj.save()
			request.session['name'] = name
			return HttpResponseRedirect('/resume/my')
		else:
			return HttpResponseRedirect('/resume/my')


class work(View):
	def get(self,request):
		context = {}
		if request.GET.get('id',''):
			id = request.GET.get('id')
			context = w.objects.filter(id = id).values()[0]
		return render(request,'work.html',context)
	def post(self,request):
		id = request.POST.get('id','')
		if id:
			post = request.POST.copy()
			post['uid'] = request.session['id']
			obj = w.objects.get(pk=id)
			f = workForm(post, instance=obj)
			#if f.is_valid():
			f.save()
			#else:
				#print('ccc')
		else:
			# import ipdb; ipdb.set_trace()
			f = workForm(request.POST)
			if f.is_valid():
				obj = f.save(commit=False)
				obj.uid_id = request.session['id']
				f.save()
			else:
				print('cccc')
		return HttpResponseRedirect('/resume/my')


class education(View):
	def get(self,request):
		context = {}
		if request.GET.get('id',''):
			id = request.GET.get('id')
			context = e.objects.filter(id = id).values()[0]
		context['educationList'] = e._meta.get_field('education').choices
		# import ipdb; ipdb.set_trace()
		return render(request,'education.html',context)
	def post(self,request):
		id = request.POST.get('id','')
		if id:
			a = e.objects.get(pk=id)
			f = educationForm(request.POST, instance=a)
			f.save()
		else:
			f = educationForm(request.POST)

			if f.is_valid():
				obj = f.save(commit=False)
				obj.uid_id = request.session['id']
				f.save()	
			else:
				print('cccc')
		return HttpResponseRedirect('/resume/my')

	
def delete(request):
		id = request.GET.get('id','')
		type = request.GET.get('type','')
		if id:
			if type == 'work':
				obj = w.objects.get(id=id)
				obj.delete()
			else:
				obj = e.objects.get(id=id)
				obj.delete()
		return HttpResponseRedirect('/resume/my')


def pdf(request):
	context = {}
	uid = request.session['id']
	context = member.objects.filter(uid = uid).values()[0]
	context['education'] = e.objects.filter(uid = uid)
	context['work'] = w.objects.filter(uid = uid)
	# data = render(request,'my.html',context).content()
	response = HttpResponse('ccc', mimetype='application/pdf')
	response['Content-Disposition'] = 'inline;filename=some_file.pdf'
	return response

def preview(request,id):
	context = {}
	if request.session['type'] == 1:
		uid = request.session['id']
	else:
		uid = id
		context.update(deliver.objects.filter(comefrom_id=id,to_id=request.session['id']).values("status")[0])
	
	obj = member.objects.filter(uid = uid)
	if obj:
		context.update(obj.values()[0])
		context['person'] = member.objects.get(uid = uid)
		context['education'] = e.objects.filter(uid = uid)
		context['work'] = w.objects.filter(uid = uid)
		context['age'] = date.today().year - context['birthday'].year
	return render(request,'preview.html',context)

def gather(request,id):
	to = request.session['id']
	deliver.objects.filter(comefrom_id=id,to_id=to).update(status=4)
	return HttpResponseRedirect('/company/collect/')

def inappropriate(request,id):
	to = request.session['id']
	deliver.objects.filter(comefrom_id=id,to_id=to).update(status=2)
	return HttpResponseRedirect('/message/')

def collectList(request):
	context = {}
	id = request.session['id']
	context['list'] = deliver.objects.filter(to_id = id,status = 4).select_related('comefrom')
	# import ipdb; ipdb.set_trace()
	return render(request,'collect.html',context)



















